﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using AutoLoL.Entities;
using AutoLoL.Logic.Parsers;
using AutoLoL.Logic.Utils;
using AutoLoL.Shared;

namespace AutoLoL.Logic.Repositories
{
    public class LocalChampionRepository : IChampionRepository
    {
        private ReadOnlyCollection<Champion> _champions;

        public LocalChampionRepository()
        {
        }

        public Champion GetByName(string championName)
        {
            if (string.IsNullOrEmpty(championName))
            {
                return null;
            }

            return GetAll().Where(c => c.Name.ToLower().Equals(championName.ToLower())).SingleOrDefault();
        }

        public ReadOnlyCollection<Champion> GetAll()
        {
            if (_champions != null)
            {
                return _champions;
            }

            List<Champion> champions = new List<Champion>();

            if (File.Exists(Constants.CHAMPIONS_XML_FILE))
            {
                XDocument championsDoc = XDocument.Load(Constants.CHAMPIONS_XML_FILE);
                foreach (XElement championElement in championsDoc.Root.Elements("champion"))
                {
                    string directory = championElement.Element("directory").Value;

                    string name = championElement.Element("name").Value;
                    string icon = championElement.Element("icon").Value;
                    string description = championElement.Element("description").Value;
                    string lore = championElement.Element("lore").Value;

                    string championDirectory = Path.Combine(Constants.CHAMPIONS_DIRECTORY, directory);
                    string iconPath = Path.Combine(championDirectory, icon);

                    Champion champion = new Champion();
                    champion.Name = name;
                    champion.IconBitmapImage = ImageParser.FileToBitmapImage(iconPath);
                    champion.Description = description;
                    champion.Lore = lore;
                    
                    List<Skin> skins = new List<Skin>();
                    if (championElement.Element("skins") != null)
                    {
                        foreach (XElement skinElement in championElement.Element("skins").Descendants())
                        {
                            string skinName = skinElement.Attribute("name").Value;
                            string obj = skinElement.Attribute("obj").Value;
                            string jpeg = skinElement.Attribute("jpeg").Value;

                            string objPath = Path.Combine(championDirectory, obj);
                            string jpegPath = Path.Combine(championDirectory, jpeg);

                            Skin skin = new Skin(skinName, objPath, jpegPath);
                            skins.Add(skin);
                        }
                    }

                    champion.Skins = new ReadOnlyCollection<Skin>(skins);
                    champions.Add(champion);
                }
            }

            champions.Sort((x1, x2) => x1.Name.CompareTo(x2.Name));
            _champions = new ReadOnlyCollection<Champion>(champions);

            return _champions;
        }


        public void SetSkins(Champion champion)
        {
            throw new NotImplementedException();
        }
    }
}